<?php

class Subjects extends Model {
	
	function Subjects()
	{
		parent::Model();
	}

	function get_top_10()
	{
		$query = $this->db->query('SELECT `SubjectName` , `SubjectTitle` FROM `Subject` where Rating != 0 ORDER BY `Rating` DESC LIMIT 0 , 10');
		return $query;
	}
	
	function update_rating($subject, $rating)
	{
		$a = $this->db->query('SELECT Rating, RateCount from Subject where Subject.SubjectName = ?', $subject);
		$count = $a['RateCount'];
		$oldRating = $a['Rating'];
		$newRating = ($rating + ($count * $oldRating))/($count + 1);
		$count = $count + 1;
		$array = array('RateCount'=>$count, 'Rating'=>$newRating);
		$this->db->where('SubjectName', $subject);
		$this->db->update('Subject', $array);
	}
	
	function select_subject($subject)
	{
		$this->db->where('SubjectName', $subject);
		$this->db->from('Subject');
		$query = $this->db->get();
		return $query;
	}
}

?>
